package com.fudan.framework.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fudan.framework.entity.RequestEntity;
import com.fudan.framework.entity.ResponseMsg;
import com.fudan.framework.utils.EntityUtils;

/**
 * 更新接口
 * @author Xuxinxing
 *
 * @param <T>
 */
public interface UpdateController<T extends RequestEntity> extends FunctionalController<T>{
    @RequestMapping("/update")
    @ResponseBody
    default public ResponseMsg doUpdate(HttpServletRequest request) throws Exception {
        checkPermission("update");
        try {
            Boolean result = update(request);
            if (result) {
                return ResponseMsg.build(SUCCESS_CODE);
            } else {
                return ResponseMsg.build(UPDATE_ERROR_CODE, "更新失败");
            }
        } catch (Exception e) {
            return ResponseMsg.build(SYSTEM_ERROR_CODE, e.getMessage());
        }
    }

    default public Boolean update(HttpServletRequest request) throws Exception {
        T entity = EntityUtils.toEntity(request, getActualClass(UpdateController.class));
        boolean result = injectService().update(entity);
        return result;
    }
}
